package toy.keli.picmap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import toy.keli.picmap.model.db;

//Android标题栏中添加返回按钮功能  https://www.jb51.net/article/111290.htm
public class CustomTitle {
    private static Activity mActivity;
    public static void getCustomTitle(Activity activity, String title) {
        mActivity = activity;

        mActivity.setContentView(R.layout.custom_title);
        mActivity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.custom_title);
        mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        TextView textView = (TextView) activity.findViewById(R.id.head_center_text);
        textView.setText(title);
        Button titleBackBtn = (Button) activity.findViewById(R.id.TitleBackBtn);
        titleBackBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("Title back","key down");

                mActivity.finish();
            }
        });
    }

    public static void Show(Context context, boolean back, String title, boolean redo,boolean home) {
        mActivity = (Activity)context;

        mActivity.setContentView(R.layout.custom_title);
        mActivity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.custom_title);
//        mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);


        Button titleBackBtn = (Button) mActivity.findViewById(R.id.TitleBackBtn);
        TextView textView = (TextView) mActivity.findViewById(R.id.head_center_text);
        Button resetBtn = (Button) mActivity.findViewById(R.id.reset);
        Button mapBtn = (Button) mActivity.findViewById(R.id.map);

        textView.setText(title);
        titleBackBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        resetBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new db(mActivity).clearAll();
                mActivity.recreate();
            }
        });
        mapBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mActivity.startActivity(new Intent(mActivity, BmapActivity.class));
            }
        });

        if(!back){
            titleBackBtn.setVisibility(View.GONE);
        }

        if(title==null){
            textView.setVisibility(View.GONE);
        }

        if(!redo){
            textView.setVisibility(View.GONE);
        }

        if(!home){
            textView.setVisibility(View.GONE);
        }
    }
}
